package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.liondance.LionDanceConfig;
import com.yanqu.road.entity.activity.liondance.LionDanceUserApplyData;
import com.yanqu.road.entity.activity.liondance.Pile;
import com.yanqu.road.entity.activity.liondance.UserLionDanceData;
import com.yanqu.road.entity.activity.liondance.config.LionDanceCardInfo;
import com.yanqu.road.entity.activity.liondance.config.LionDanceCombInfo;
import com.yanqu.road.entity.activity.liondance.config.LionDanceJumpConfigInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.LionDanceProto;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.liondance.LionDanceMgr;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LionDanceActivityPb {


    public static LionDanceProto.LionDanceUserDataTemp.Builder buildUserData(UserLionDanceData userData) {

        LionDanceProto.LionDanceUserDataTemp.Builder builder = LionDanceProto.LionDanceUserDataTemp.newBuilder();
        builder.setMakeNum(userData.getMakeNum());
        builder.setProcess(userData.getProcess());
        builder.setScore(userData.getScore());
        builder.setScoreAddition(0);
        builder.setMaxRank(userData.getMaxRank());
        LionDanceConfig config = LionDanceMgr.getConfig();
        if(config != null){
            builder.setScoreAddition(LionDanceMgr.getMakeLionAddition(userData.getMakeNum(), userData.getProcess(), config));
        }
        builder.setMaxScore(userData.getMaxScore());
        if(userData.getStatus() != null){
            LionDanceProto.LionDanceGameStatusTemp.Builder statusBuilder = LionDanceProto.LionDanceGameStatusTemp.newBuilder();
            statusBuilder.setStep(userData.getStatus().getStep());
            statusBuilder.setSettle(userData.getStatus().isSettle());
            statusBuilder.setAddition(userData.getStatus().getAddition());
            statusBuilder.setAllGoodsNum(userData.getStatus().getAllGoodsNum());
            statusBuilder.setGetNum(userData.getStatus().getGetNum());
            statusBuilder.setAdditionTime(userData.getStatus().getAdditionTime());
            statusBuilder.setScore(userData.getStatus().getScore());
            statusBuilder.addAllHandle(userData.getStatus().getStepList());
            for (Map.Entry<Integer, Integer> entry : userData.getStatus().getGetNumMap().entrySet()) {
                LionDanceProto.LionDanceKeyValueTemp.Builder itemBuilder = LionDanceProto.LionDanceKeyValueTemp.newBuilder();
                itemBuilder.setItemId(entry.getKey());
                itemBuilder.setNum(entry.getValue());
                statusBuilder.addCardMap(itemBuilder);
            }
            builder.setStatus(statusBuilder);
        }

        for (Map.Entry<Integer, BigInteger> entry : userData.getCardMap().getGoods().entrySet()) {
            LionDanceProto.LionDanceKeyValueTemp.Builder itemBuilder = LionDanceProto.LionDanceKeyValueTemp.newBuilder();
            itemBuilder.setItemId(entry.getKey());
            itemBuilder.setNum(entry.getValue().intValue());
            builder.addCard(itemBuilder);
        }

        for (Map.Entry<Integer, Integer> entry : userData.getCombMap().entrySet()) {
            LionDanceProto.LionDanceKeyValueTemp.Builder itemBuilder = LionDanceProto.LionDanceKeyValueTemp.newBuilder();
            itemBuilder.setItemId(entry.getKey());
            itemBuilder.setNum(entry.getValue());
            builder.addComb(itemBuilder);
        }

        for (Pile pile : userData.getPileList()) {
            LionDanceProto.LionDancePassPileTemp.Builder passBuilder = LionDanceProto.LionDancePassPileTemp.newBuilder();
            passBuilder.setType(pile.getType());
            passBuilder.setCType(pile.getcType());
            passBuilder.setFType(pile.getfType());
            passBuilder.setGap(pile.getGap());
            passBuilder.setGId(pile.getgId());
            passBuilder.setMove(pile.getMove());
            passBuilder.setNum(pile.getNum());
            passBuilder.setP(pile.getP());
            passBuilder.setColumnHigh(pile.getcHigh());
            passBuilder.setAId(pile.getaId());
            builder.addPass(passBuilder);
        }

        return builder;
    }

    public static LionDanceProto.LionDanceUserApplyTemp.Builder buildApplyData(LionDanceUserApplyData applyData) {
        LionDanceProto.LionDanceUserApplyTemp.Builder builder = LionDanceProto.LionDanceUserApplyTemp.newBuilder();
        builder.setServerId(applyData.getServerId());
        builder.setApplyUserId(applyData.getUserId());
        builder.setApplyNo(applyData.getApplyNo());
        builder.setApplyGoodsId(applyData.getApplyGoodsId());
        builder.setApplyTimes(applyData.getApplyTimes());
        builder.setFriendId(applyData.getFriendId());
        builder.setStatus(applyData.getStatus());
        if(applyData.getFriend() != null){
            builder.setFriend(PlayerBasePb.parsePlayerBaseTempMsg(applyData.getFriend()));
        }
        return builder;
    }

    public static List<LionDanceProto.LionDanceUserApplyTemp> buildApplyDataList(List<LionDanceUserApplyData> list) {
        List<LionDanceProto.LionDanceUserApplyTemp> result = new ArrayList<>();
        for (LionDanceUserApplyData applyData : list) {
            result.add(buildApplyData(applyData).build());
        }
        return result;
    }

    public static List<LionDanceProto.LionDanceCardConfigTemp> buildCardListTemp(List<LionDanceCardInfo> cardInfoList, String language) {
        List<LionDanceProto.LionDanceCardConfigTemp> list = new ArrayList<>();
        for (LionDanceCardInfo cardInfo : cardInfoList) {
            list.add(buildCartTemp(cardInfo, language).build());
        }
        return list;
    }

    private static LionDanceProto.LionDanceCardConfigTemp.Builder buildCartTemp(LionDanceCardInfo cardInfo, String language) {
        LionDanceProto.LionDanceCardConfigTemp.Builder builder = LionDanceProto.LionDanceCardConfigTemp.newBuilder();
        builder.setId(cardInfo.getId());
        builder.setName(ServerLanguageMgr.getContent(cardInfo.getName(), language));
        builder.setDesc(ServerLanguageMgr.getContent(cardInfo.getDesc(), language));
        return builder;
    }

    public static List<LionDanceProto.LionDanceCombConfigTemp> buildCombListTemp(List<LionDanceCombInfo> combInfoList, String language) {
        List<LionDanceProto.LionDanceCombConfigTemp> list = new ArrayList<>();
        for (LionDanceCombInfo combInfo : combInfoList) {
            list.add(buildCombTemp(combInfo, language).build());
        }
        return list;
    }

    private static LionDanceProto.LionDanceCombConfigTemp.Builder buildCombTemp(LionDanceCombInfo combInfo, String language) {
        LionDanceProto.LionDanceCombConfigTemp.Builder builder = LionDanceProto.LionDanceCombConfigTemp.newBuilder();
        builder.setCombId(combInfo.getCombId());
        builder.setElement(PropertyHelper.parsePropertyToString(combInfo.getElement()));
        builder.setName(ServerLanguageMgr.getContent(combInfo.getName(), language));
        builder.setReward(combInfo.getRewards());
        return builder;
    }

    public static List<LionDanceProto.LionDanceJumpConfigTemp> buildJumpListTemp(List<LionDanceJumpConfigInfo> jumpConfigInfoList) {
        List<LionDanceProto.LionDanceJumpConfigTemp> list = new ArrayList<>();
        for (LionDanceJumpConfigInfo jumpConfigInfo : jumpConfigInfoList) {
            list.add(buildJumpTemp(jumpConfigInfo).build());
        }
        return list;
    }

    private static LionDanceProto.LionDanceJumpConfigTemp.Builder buildJumpTemp(LionDanceJumpConfigInfo jumpConfigInfo) {
        LionDanceProto.LionDanceJumpConfigTemp.Builder builder = LionDanceProto.LionDanceJumpConfigTemp.newBuilder();
        builder.setId(jumpConfigInfo.getId());
        builder.setPileNum(jumpConfigInfo.getPileNum());
        builder.addAllPileFace(jumpConfigInfo.getFackWeightList());
        builder.addAllPileColumn(jumpConfigInfo.getColumnWeightList());
        builder.addAllDynamic(jumpConfigInfo.getDynamicWeightList());
        builder.addAllColorPilePosition(jumpConfigInfo.getColorNumList());
        builder.addAllAddItemPosition(jumpConfigInfo.getAddItemPosition());
        builder.setAddItem(jumpConfigInfo.getAddItem());
        builder.addAllCollectItemPosition(jumpConfigInfo.getCollectItemPosition());
        builder.setCollectItem(jumpConfigInfo.getCollectItem());
        builder.addAllCollectItemType(jumpConfigInfo.getCollectItemType());
        builder.addAllFlowerPileDistance(jumpConfigInfo.getGapRangeList());
        builder.addAllPileColumn(jumpConfigInfo.getPileHighWeighList());
        return builder;
    }
}
